package com.liujit.standard.basis.core.domain.result;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 列表 返回结果列表封装
 * @Author LiuJun
 * @Date 2020/8/12 4:24 下午
 */
@Data
@NoArgsConstructor
@ApiModel("列表 返回结果列表封装")
@JsonPropertyOrder({"pageNumber", "pageSize", "total", "pages", "size", "rows"})
public class PageBean<T> implements Serializable {

    private static final long serialVersionUID = 1000L;

    @ApiModelProperty("总记录数")
    private Integer total;

    @ApiModelProperty("结果集")
    private List<T> rows = new ArrayList<>();

    @ApiModelProperty("第几页")
    private int pageNumber;

    @ApiModelProperty("每页记录数")
    private int pageSize;

    @ApiModelProperty("总页数")
    private int pages;

    @ApiModelProperty("当前页的数量<=pageSize")
    private int size;

    public PageBean(List list) {
        if (list != null && list instanceof Page) {
            Page page = (Page) list;
            this.pageNumber = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = Long.valueOf(page.getTotal()).intValue();
            this.pages = page.getPages();
            this.rows = list == null ? new ArrayList<>() : (List) list;
            this.size = this.rows.size();
        }
    }
}
